package server.db;

import server.ServerConfiguration;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.messaging.MessageSender;

/**
 * @author db4o Tutorial und Julius Ollesch
 * 
 *         stops the db4o Server started with {@link DBServer}. <br>
 * <br>
 *         This is done by opening a client connection to the server and by
 *         sending a StopServer object as a message. {@link DBServer} will react
 *         in it's processMessage method.
 */
public class StopDBServer implements ServerConfiguration {
	/**
	 * stops a db4o Server started with StartServer.
	 * 
	 * @throws Exception
	 */
	private static final String name = "DB4O Server: "; //$NON-NLS-1$

	public static void main(String[] args) {
		ObjectContainer objectContainer = null;
		try {

			// connect to the server
			objectContainer = Db4o.openClient(HOST, PORT, USER, PASS);

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (objectContainer != null) {

			// get the messageSender for the ObjectContainer
			MessageSender messageSender = objectContainer.ext().configure()
					.clientServer().getMessageSender();

			// send an instance of a StopServer object
			messageSender.send(new StopDBServer());

			System.out.println(name
					+ Messages.getString("StopDBServer.Messages.Stopped")); //$NON-NLS-1$

			// close the ObjectContainer
			objectContainer.close();
		}
	}
}
